package models

import (
	"context"
	"go.mongodb.org/mongo-driver/mongo"
	"go.mongodb.org/mongo-driver/mongo/options"
	"log"
)

var Mongo *mongo.Database

func InitMongoDb(authsource string, username string, password string) {
	// 设置客户端连接配置
	credential := options.Credential{
		AuthSource: authsource,
		Username:   username,
		Password:   password,
	}
	clientOptions := options.Client().ApplyURI("mongodb://182.61.14.195:27017").SetAuth(credential)
	// 连接到MongoDB
	client, err := mongo.Connect(context.TODO(), clientOptions)
	if err != nil {
		log.Fatal(err)
	}
	// 检查连接
	err = client.Ping(context.TODO(), nil)
	if err != nil {
		log.Fatal(err)
	}
	//连接指定数据库
	Mongo = client.Database("monitoring_system")
}
